package com.example.client_anna.util.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.client_anna.R;
import com.example.client_anna.module.branch_explore.ExploreDetailsActivity;
import com.example.client_anna.util.entity.Explore_data;
import com.example.client_anna.network.okhttp3.RequestUrl;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/10/19
 */
public class ExploreAdapter extends RecyclerView.Adapter<ExploreAdapter.ViewHolder>  {

    private Context mContext;
    private List<Explore_data> mExplore;

    //适配器通过onCreateViewHolder()方法与xml布局文件explore_data.xml绑定
    //适配器通过类ViewHoldery与布局文件中的控件进行绑定
    //适配器通过onBindViewHolder()方法与数据源进行绑定
    //explore_data.xml——onCreateViewHolder()方法——类ViewHoldery——onBindViewHolder()方法

    //viewholder与xml布局中的控件关联
    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView exploreimage;
        TextView exploretheme;
        TextView exploredetail;
        public ViewHolder(View view){
            super(view);
            cardView = (CardView) view;
            exploreimage = (ImageView) view.findViewById(R.id.explore_image);
            exploretheme = (TextView) view.findViewById(R.id.explore_theme);
            exploredetail = (TextView) view.findViewById(R.id.explore_detail);

        }
    }

    //为适配器传入数据
    public ExploreAdapter(List<Explore_data> explore_dataList){
        mExplore = explore_dataList;
    }

    //找到需要具体显示内容的xml布局文件，
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int i) {
        if (mContext == null){
            mContext = viewGroup.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.explore_data,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);

        //卡片点击事件
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Explore_data explore_data = mExplore.get(position);
                Intent intent = new Intent(mContext, ExploreDetailsActivity.class);
                //用来获取分享该信息的用户信息
                intent.putExtra(ExploreDetailsActivity.USER_ID,explore_data.getUserid());
                //用来申请服务器上该分享信息的所有评论
                intent.putExtra(ExploreDetailsActivity.EXPLORE_ID,explore_data.getExploreid());
                intent.putExtra(ExploreDetailsActivity.EXPLORE_THEME,explore_data.getTheme());
                intent.putExtra(ExploreDetailsActivity.IMAGES_URL,RequestUrl.getUrl()+
                        explore_data.getImageurl());
                intent.putExtra(ExploreDetailsActivity.EXPLORE_STORY,explore_data.getStory());
                mContext.startActivity(intent);
            }
        });

        return holder;
    }

    //数据源与ViewHolder进行绑定
    //可以实现单独卡片的事件监听
    @Override
    public void onBindViewHolder(@NonNull @NotNull ExploreAdapter.ViewHolder viewHolder, int i) {
        Explore_data explore_data = mExplore.get(i);
        viewHolder.exploretheme.setText(explore_data.getTheme());
        viewHolder.exploredetail.setText(explore_data.getDetail());
        String url = RequestUrl.getUrl()+explore_data.getImageurl();
        displayImageCenter(mContext,viewHolder.exploreimage,url);
    }

    public static void displayImageCenter(Context context,final ImageView imageView,String url){
        RequestOptions options = new RequestOptions()
                //磁盘缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                //内存缓存，默认false，表示不跳过内存缓存
                .skipMemoryCache(false)
                //加载错误显示的图片
                .error(R.drawable.explore_error)
                //占位图
                .placeholder(R.drawable.explore_placeholder)
                //设置图片的宽和高，获取imageView控件的宽度和高
                //注：在onCreate中是获取不到imageview的高度的，因为这个时候imageView控件自己都还没有测量
                .override(imageView.getMeasuredWidth(), imageView.getMeasuredHeight())
                //不需要动画效果，默认300
                .dontAnimate();
        //Glide缓存顺序：内存>磁盘>网络
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //获取数据大小，长度，数量
    @Override
    public int getItemCount() {
        return mExplore.size();
    }

    //下拉加载方法_mFruitList添加最新的数据使用notifyDataSetChanged方法更新适配器
    public void addLoadItem( List<Explore_data> mDataList){
        mExplore.addAll(mDataList);
        //notifyDataSetChanged();
    }

}
